package com.tool.common.utils;

import cn.hutool.core.text.CharSequenceUtil;

/**
 * @author: zhoubin
 * @date: 2024/1/10
 */
public class CalculatorUtil {

    public static void main(String[] args) {
        System.out.println(calcPercent(101d, "-0.01%"));
    }

    /**
     * 使用百分比计算
     * @param origin 原数据
     * @param percent 百分比
     * @return
     */
    public static double calcPercent(double origin, String percent) {

        if (origin == 0 || CharSequenceUtil.isBlank(percent)) {
            return 0;
        }

        String num = percent.substring(0, percent.length() - 1);
        double ratio;

        if (num.startsWith("-")) {
            ratio = (100d - Double.parseDouble(num.substring(1))) / 100;
        } else {
            ratio = (100d + Double.parseDouble(num)) / 100;
        }

        return origin * ratio;
    }

    /**
     * 计算降低了多少
     * @param origin 当前值
     * @param percent 百分比
     * @return
     */
    public static double calcEnhance(double origin, String percent) {

        String num = percent.substring(0, percent.length() - 1);

        return origin + Double.parseDouble(num);
    }
}
